package com.itedge.solutionmanager.domain.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Version;
import javax.validation.constraints.Size;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.itedge.solutionmanager.taskdata.impl.solution.ScheduleMeetingTaskData;

@Entity
@Table(name="EMPLOYEE")
public class Employee extends AbstractPerson implements UserDetails {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;	

	@Version
    @Column(name = "version")
    private Integer version;
	
	@Column(name ="USERNAME")
	@Size(max = 255)
	private String username;
	
	@Column(name ="PASSWORD")
	@Size(max = 255)
	private String password;
	
	@Column(name ="ENABLED")
	private Boolean enabled;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name="EMPLOYEES_AUTHORITIES",
            joinColumns=@JoinColumn(name="EMPLOYEE_ID"),
            inverseJoinColumns=@JoinColumn(name="AUTHORITY_ID")
        )
    private Set<EmployeeAuthority> emplAuthorities = new HashSet<EmployeeAuthority>(); 
	
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name="SOLUTIONS_EMPLOYEES",
            joinColumns=@JoinColumn(name="EMPLOYEE_ID"),
            inverseJoinColumns=@JoinColumn(name="SOLUTION_ID")
        )
    private Set<Solution> solutions = new HashSet<Solution>(); 	
    
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name="MEETING_TASKS_EMPLOYEES",
            joinColumns=@JoinColumn(name="EMPLOYEE_ID"),
            inverseJoinColumns=@JoinColumn(name="MEETING_TASK_ID")
        )
    private Set<ScheduleMeetingTaskData> meetingTasks = new HashSet<ScheduleMeetingTaskData>();
	
	public Set<Solution> getSolutions() {
		return solutions;
	}

	public void setSolutions(Set<Solution> solutions) {
		this.solutions = solutions;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Set<ScheduleMeetingTaskData> getMeetingTasks() {
		return meetingTasks;
	}

	public void setMeetingTasks(Set<ScheduleMeetingTaskData> meetingTasks) {
		this.meetingTasks = meetingTasks;
	}

	@Override
	public Long getId() {
        return this.id;
    }	

	@Override
	public Integer getVersion() {
        return this.version;
    }
	
	public void setId(Long id) {
		this.id = id;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Set<EmployeeAuthority> getEmplAuthorities() {
		return emplAuthorities;
	}

	public void setEmplAuthorities(Set<EmployeeAuthority> emplAuthorities) {
		this.emplAuthorities = emplAuthorities;
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		Iterator<EmployeeAuthority> it = getEmplAuthorities().iterator();
		List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
		while(it.hasNext()) {
			grantedAuthorities.add(it.next());
		}
		return grantedAuthorities;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public boolean isEnabled() {
		return this.enabled;
	}
	
	public void setEnabled(boolean value) {
		this.enabled = value;
	}
	
	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

}